package earogya.ehr.medical_records.messages;

import java.io.IOException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.message.ADT_A61;
import ca.uhn.hl7v2.model.v251.segment.MSH;
import ca.uhn.hl7v2.model.v251.segment.PID;
import ca.uhn.hl7v2.model.v251.segment.PV1;
import earogya.ehr.medical_records.ConsultantChangeLog;
import earogya.ehr.medical_records.HibernateManager;

/***
 * Change consulting doctor
 * @author Chamendri
 *
 */
public class ADT_61_Handler {

	private ADT_A61 adtMsg;
	private HibernateManager hibernate_manager=new HibernateManager();
	
	public ADT_61_Handler(ADT_A61 adtMsg){
		this.adtMsg=adtMsg;
	}
	
	public String manageMessage() throws DataTypeException, HL7Exception, IOException{
		String ack=null;
		PV1 pv1=adtMsg.getPV1();
		int admission_id=this.getAdmissionId();
		if(admission_id!=-1){
			String previous_consultant_id=this.getConsultantId(admission_id);
			String new_consultant_id=pv1.getConsultingDoctor(0).getIDNumber().getValue();
			Boolean isSuccesfulLog=this.addToConsultantLog(admission_id, new_consultant_id, previous_consultant_id);
			Boolean isSuccesfulUpdate=this.updateBht(admission_id, new_consultant_id);
			if(isSuccesfulLog && isSuccesfulUpdate){
				ack=adtMsg.generateACK().encode();
			}
		}
		else{
			HL7Exception ex=new HL7Exception("Incorrect patient ID");
			ack=adtMsg.generateACK("AR", ex).encode();
		}
		return ack;
	}
	
	private int getAdmissionId()throws DataTypeException{
		int admission_id=-1;
		PID pid=adtMsg.getPID();
		int patient_id=Integer.parseInt(pid.getPatientID().getIDNumber().getValue());
		Object[][] fieldsAndValues={{"patient_id", patient_id}, {"status", "in"}};
		admission_id=hibernate_manager.queryValuesFromDatabase(fieldsAndValues, "admission_id", "Admission", "admission"); 
		return admission_id;
	}
	
	
	private Boolean addToConsultantLog(int admission_id, String new_consultant_id, String previous_consultant_id) throws DataTypeException{
		Boolean isSuccessful=false;
		MSH msh=adtMsg.getMSH();
		ConsultantChangeLog changeLog=new ConsultantChangeLog();
		changeLog.setNew_consultant_id(new_consultant_id);
		changeLog.setAdmission_id(admission_id);
		changeLog.setChange_date(msh.getDateTimeOfMessage().getTime().getValueAsDate());
		changeLog.setPrevious_consultant_id(previous_consultant_id);
		isSuccessful=hibernate_manager.saveObject(changeLog);
		return isSuccessful;
	}
	
	private String getConsultantId(int admission_id){
		String consultant_id=null;
		Object[][] fieldsAndValues={{"admission_id", admission_id}};
		consultant_id=hibernate_manager.queryValuesFromDatabaseString(fieldsAndValues, "consultant_id", "Bht", "bht");
		return consultant_id;
	}
	
	private Boolean updateBht(int admission_id, String new_consultant_id){
		Boolean isSuccesful=false;
		Object[][] fieldsAndValues={{"admission_id", admission_id}};
		int bht_id=hibernate_manager.queryValuesFromDatabase(fieldsAndValues, "bht_id", "Bht", "bht");
		if(bht_id!=-1){
			isSuccesful=hibernate_manager.updateConsultant(bht_id, new_consultant_id);
		}
		return isSuccesful;
	}
}
